package com.yangyuhao.haowuyoupin;

import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;

public class ProductDetailActivity extends AppCompatActivity {

    public static final String EXTRA_PRODUCT_IMAGE = "product_image";
    public static final String EXTRA_PRODUCT_TITLE = "product_title";
    public static final String EXTRA_PRODUCT_PRICE = "product_price";
    public static final String EXTRA_PRODUCT_UNIT = "product_unit";
    public static final String EXTRA_PRODUCT_DESCRIPTION = "product_description";

    private VideoView videoView;
    private ImageButton playPauseButton;
    private SeekBar videoProgress;
    private TextView videoDuration;
    private ProgressBar loadingProgress;
    private Handler handler;
    private boolean isPlaying = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);

        // 初始化视频相关控件
        videoView = findViewById(R.id.product_video);
        playPauseButton = findViewById(R.id.btn_play_pause);
        videoProgress = findViewById(R.id.video_progress);
        videoDuration = findViewById(R.id.video_duration);
        loadingProgress = findViewById(R.id.loading_progress);
        handler = new Handler();

        // 设置视频源
        try {
            Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video);
            videoView.setVideoURI(videoUri);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "视频加载失败", Toast.LENGTH_SHORT).show();
        }

        // 设置视频控制
        setupVideoControls();

        // 获取传递过来的商品数据
        String title = getIntent().getStringExtra(EXTRA_PRODUCT_TITLE);
        String price = getIntent().getStringExtra(EXTRA_PRODUCT_PRICE);
        String unit = getIntent().getStringExtra(EXTRA_PRODUCT_UNIT);
        String description = getIntent().getStringExtra(EXTRA_PRODUCT_DESCRIPTION);

        // 设置返回按钮
        ImageView btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(v -> finish());

        // 设置商品信息
        TextView productTitle = findViewById(R.id.product_title);
        TextView productPrice = findViewById(R.id.product_price);
        TextView productUnit = findViewById(R.id.product_unit);
        TextView productDescription = findViewById(R.id.product_description);

        productTitle.setText(title);
        productPrice.setText(price);
        productUnit.setText(unit);
        productDescription.setText(description);

        // 设置原价的删除线
        TextView originalPrice = findViewById(R.id.original_price);
        originalPrice.setPaintFlags(originalPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    }

    private void setupVideoControls() {
        // 视频准备完成的监听器
        videoView.setOnPreparedListener(mp -> {
            loadingProgress.setVisibility(View.GONE);
            int duration = videoView.getDuration();
            videoProgress.setMax(duration);
            videoDuration.setText(formatDuration(duration));
            
            // 设置视频循环播放
            mp.setLooping(true);
            
            // 自动开始播放
            videoView.start();
            playPauseButton.setImageResource(android.R.drawable.ic_media_pause);
            isPlaying = true;
            startProgressUpdate();
        });

        // 视频错误监听器
        videoView.setOnErrorListener((mp, what, extra) -> {
            loadingProgress.setVisibility(View.GONE);
            Toast.makeText(this, "视频播放出错", Toast.LENGTH_SHORT).show();
            return true;
        });

        // 播放/暂停按钮点击事件
        playPauseButton.setOnClickListener(v -> {
            if (isPlaying) {
                videoView.pause();
                playPauseButton.setImageResource(android.R.drawable.ic_media_play);
            } else {
                videoView.start();
                playPauseButton.setImageResource(android.R.drawable.ic_media_pause);
                startProgressUpdate();
            }
            isPlaying = !isPlaying;
        });

        // 进度条拖动监听器
        videoProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    videoView.seekTo(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                videoView.pause();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                videoView.start();
                isPlaying = true;
                playPauseButton.setImageResource(android.R.drawable.ic_media_pause);
            }
        });
    }

    private void startProgressUpdate() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (videoView != null && videoView.isPlaying()) {
                    int currentPosition = videoView.getCurrentPosition();
                    videoProgress.setProgress(currentPosition);
                    handler.postDelayed(this, 500);
                }
            }
        }, 500);
    }

    private String formatDuration(int duration) {
        int seconds = (duration / 1000) % 60;
        int minutes = (duration / 1000) / 60;
        return String.format("%02d:%02d", minutes, seconds);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 恢复播放
        if (isPlaying) {
            videoView.start();
            startProgressUpdate();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 暂停播放
        if (videoView.isPlaying()) {
            videoView.pause();
            handler.removeCallbacksAndMessages(null);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (videoView != null) {
            videoView.stopPlayback();
        }
        handler.removeCallbacksAndMessages(null);
    }
} 